package com.dimong.www.loan.ui.act.account;

import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.loan.sdk.ui.WeakHandler;
import com.dimong.loan.sdk.utils.SharedUtil;
import com.dimong.www.loan.R;
import com.dimong.www.loan.cache.UserIndexPageCache;
import com.dimong.www.loan.net.LoadHttpUtil;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.ui.base.BaseRefreshActivity;

public class RegisterFirstActivity extends BaseRefreshActivity implements View.OnClickListener {
    private EditText register_phone;
    private EditText register_verification_code;
    private TextView retrieve_pass_send_identify;
    private Button bt_agreement;
    private Button register_submit;

    private int time = 60;
    private String phone;
    private String code;

    private WeakHandler weakHandler;

    @Override
    public void initIntentParam(Intent intent) {

    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        register_phone = findViewById(R.id.register_phone);
        retrieve_pass_send_identify = findViewById(R.id.retrieve_pass_send_identify);
        register_verification_code = findViewById(R.id.register_verification_code);
        bt_agreement = findViewById(R.id.bt_agreement);
        register_submit = findViewById(R.id.register_submit);
    }

    @Override
    public void setViewStatus() {
        retrieve_pass_send_identify.setOnClickListener(this);
        bt_agreement.setOnClickListener(this);
        register_submit.setOnClickListener(this);

        weakHandler = new WeakHandler(this);
    }

    @Override
    public void onActResume() {

    }

    @Override
    public void onActPause() {

    }

    @Override
    public void onActDestroy() {

    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retrieve_pass_send_identify:
                if (!verifyInfo())
                    return;
                LoadHttpUtil.sendSms(phone, new NetHttpCallback() {
                    @Override
                    public void onPreRequest() {
                        super.onPreRequest();
                        showRefreshLoading();
                    }

                    @Override
                    public void onResult(String result) {
                        super.onResult(result);
                        showRefreshSuccess();
                        showToast("获取验证码成功");
                        time = 60;
                        register_verification_code.requestFocus();
                        retrieve_pass_send_identify.setClickable(false);
                        retrieve_pass_send_identify.setText(time + "秒");
                        weakHandler.sendEmptyMessageDelayed(0, 1000L);
                    }

                    @Override
                    public void onError(String errorCode, String error) {
                        super.onError(errorCode, error);
                        showRefreshSuccess();
                        showToast("获取验证码失败");
                    }
                });
                break;
            case R.id.bt_agreement:
                LoadHttpUtil.getUserProtocolUrl(new NetHttpCallback() {
                    @Override
                    public void onPreRequest() {
                        super.onPreRequest();
                        showRefreshLoading();
                    }

                    @Override
                    public void onResult(String result) {
                        super.onResult(result);
                        showRefreshSuccess();
                        String str = gson.fromJson(result, String.class);
                        UIHelper.openWebViewAty(RegisterFirstActivity.this, "用户协议", str);
                    }

                    @Override
                    public void onError(String errorCode, String error) {
                        super.onError(errorCode, error);
                        showRefreshSuccess();
                        showToast(error);
                    }
                });

                break;
            case R.id.register_submit:
                upMobileVerfication();
                break;
            default:
                break;
        }
    }

    private boolean verifyInfo() {
        phone = register_phone.getText().toString();
        if (phone == null || phone.length() != 11) {
            showToast("请填写手机号");
            return false;
        }
        return true;
    }

    //
    public void upMobileVerfication() {
        if (!verifyInfo()) {
            return;
        }
        code = register_verification_code.getText().toString();
        if (code == null || code.equals("") || code.length() != 6) {
            showToast("请输入正确的验证码");
            return;
        }
        LoadHttpUtil.checkUserLogin(phone, code, new NetHttpCallback() {
            @Override
            public void onPreRequest() {
                super.onPreRequest();
                showRefreshLoading();
            }

            @Override
            public void onResult(String result) {
                super.onResult(result);
                showRefreshSuccess();
                SharedUtil.setPreferStr("phone", phone);
                UserIndexPageCache.getInstance().loadCache();
                showToast("登陆成功");
                finish();
            }

            @Override
            public void onError(String errorCode, String error) {
                super.onError(errorCode, error);
                showRefreshSuccess();
                showToast(error);
            }
        });
    }

    @Override
    public void handleActivityMessage(Message msg) {
        super.handleActivityMessage(msg);
        switch (msg.what) {
            case 0:
                if (time == 0) {
                    retrieve_pass_send_identify.setClickable(true);
                    retrieve_pass_send_identify.setText("重发");
                    return;
                }
                time = -1 + time;
                retrieve_pass_send_identify.setClickable(false);
                retrieve_pass_send_identify.setText(time + "秒");
                weakHandler.sendEmptyMessageDelayed(0, 1000L);
                break;
            default:
                break;
        }
    }

    @Override
    public int getContentView() {
        return R.layout.act_register_first;
    }

    @Override
    public RelativeLayout getContainView() {
        return findViewById(R.id.rl_act_register_first);
    }
}
